package com.dhf.framework.system.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dhf.framework.base.mybatis.core.page.Pagination;
import com.dhf.framework.common.BaseController;
import com.dhf.framework.system.entity.SysMenu;
import com.dhf.framework.system.entity.SysMenuRole;
import com.dhf.framework.system.entity.SysRole;
import com.dhf.framework.system.service.SysMenuRoleService;
import com.dhf.framework.system.service.SysMenuService;
import com.dhf.framework.system.service.SysRoleService;

 /**
 * </b>SysMenuRoleController<br>
 * @author minstar
 * @since2017年03月20日 17时09分42秒 星期一 
 * @version:1.0
 */
@Controller
@RequestMapping("/system/sysMenuRole")
public class SysMenuRoleController extends BaseController{
		private static Logger LOGGER = LoggerFactory.getLogger(SysMenuRoleController.class);
	
  		@Autowired
  		private SysMenuRoleService sysMenuRoleService;
  		
		@Autowired
  		private SysRoleService sysRoleService;
		
  		@Autowired
  		private SysMenuService sysMenuService;
  		
  		/**
		  * 
		  * @Title: queryPageList
		  * @Description: 列表页面
		  * @param @param searchEntity
		  * @param @param pagination
		  * @return ModelAndView    返回类型
		  * @throws
		*/
		@RequestMapping(value = "/queryPageList")
		public ModelAndView queryPageList(SysMenuRole sysMenuRole, Pagination<SysMenuRole> pagination) {
			LOGGER.debug(SysMenuRoleController.class.getName()+":开始查询分页数据！");
			ModelAndView mv = new ModelAndView( "system/sysMenuRole/sysMenuRoleList" );
			pagination = this.sysMenuRoleService.queryPageList( sysMenuRole, pagination );
			List<SysMenuRole> menuRoleList=pagination.getItems();
			for(SysMenuRole entity:menuRoleList){
				Long roleId=entity.getRoleId();
				if(roleId!=null){
					SysRole sysRole=this.sysRoleService.get(roleId);
					entity.setRoleName(sysRole.getRoleName());
				}
				Long menuId=entity.getMenuId();
				if(menuId!=null){
					SysMenu sysMenu =this.sysMenuService.get(menuId);
					entity.setMenuName(sysMenu.getMenuName());
				}
			}
			mv.addObject( "pagination", pagination );
			List<SysRole> list=this.sysRoleService.query(null);
			mv.addObject( "list", list );
			return mv;
		}
		
		
		/**
		 * 
		 * @Title: toMerge
		 * @Description: 新增或修改页面
		 * @param @param entity
		 * @return ModelAndView    返回类型
		 * @throws
		 */
		@RequestMapping(value = "/toMerge")
		public ModelAndView toMerge(SysMenuRole entity){
			ModelAndView mv = new ModelAndView("system/sysMenuRole/sysMenuRoleMerge");
			if(entity.getId()!= null){
				SysMenuRole sysMenuRole =  this.sysMenuRoleService.get( entity.getId() ) ;
				mv.addObject("sysMenuRole",sysMenuRole);
			}else{
				mv.addObject("sysMenuRole", new SysMenuRole());
			}
			List<SysRole> roleList=this.sysRoleService.query(null);
			mv.addObject( "roleList", roleList );
			List<SysMenu> menuList=this.sysMenuService.listAll();
			mv.addObject( "menuList", menuList );
			return mv;
		}

		/**
		 * 
		* @Title: doMerge
		* @Description: 新增或修改数据 
		* @param @param request
		* @param @param entity
		* @return ModelAndView    返回类型
		* @throws
		 */
		@RequestMapping(value = "/doMerge")
		public ModelAndView doMerge(HttpServletRequest request, SysMenuRole entity,RedirectAttributes redirectAttributes) {
			if (entity.getId()!=null) {
				LOGGER.debug(SysMenuRoleController.class.getName()+":开始更新数据！");
				this.addMessage(redirectAttributes, "修改数据成功");
				this.sysMenuRoleService.update( entity );
			} else {
				LOGGER.debug(SysMenuRoleController.class.getName()+":开始新增数据！");
				this.sysMenuRoleService.add( entity );
				this.addMessage(redirectAttributes, "新增据成功");
			}
			return new ModelAndView( "redirect:/system/sysMenuRole/queryPageList.do" );
		}

		/**
		 * 
		 * @Title: delete
		 * @Description:删除数据
		 * @param @param id
		 * @return ModelAndView    返回类型
		 * @throws
		 */
		@RequestMapping(value = "/delete")
		public ModelAndView delete(Long id,RedirectAttributes redirectAttributes) {
			LOGGER.debug(SysMenuRoleController.class.getName()+":开始删除数据！");
	        this.sysMenuRoleService.delete( id );
	        this.addMessage(redirectAttributes, "删除数据成功");
	        return new ModelAndView( "redirect:/system/sysMenuRole/queryPageList.do" );
		}
}

